import Schema from 'async-validator';

// 正则
const regular = {
	// 手机号号
	phoneNumber: /^1(3|4|5|7|8|9|6)\d{9}$/,
	// 身份证号
	IDCard: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{7}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/,
};

// 验证字符串
export function checkString(message) {
	return {
		type: 'string',
		required: true,
		message: message,
	};
}

// 验证手机号
export function checkPhoneNumber(message) {
	return {
		pattern: regular.phoneNumber,
		required: true,
		message: message,
	};
}

// 封装校验方法
export function validatePlus(rule, params) {
	return new Promise((resolve) => {
		new Schema(rule)
			.validate(params)
			.then(() => {
				resolve();
			})
			.catch(({ errors }) => {
				console.log('dialog');
				console.log(errors[0].message);
			});
	});
}
